<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\modules\Finance\models\Finance;
use dektrium\user\models\User;
use yii\widgets\LinkPager;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\Finance\models\FinanceSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app.finance', 'Finances');
$this->params['breadcrumbs'][] = $this->title;

$user = [];
$u = Finance::find()->select(['user_id', 'user_id'])->groupBy('user_id')->all();

if($u){
	foreach($u as $k=>$v){
		$userModel = User::findOne($v);
		$user[$v->user_id]	= $userModel->profile->name;
	}
}


?>
<div class="finance-index">

    <h2 class="page-header"><small><?= Html::encode($this->title) ?></small>
    	<p class="pull-right">
          <?php if(Yii::$app->user->can("is_receive")){  ?>  
          <a href="/finance/receive-account/index" target="_blank" class="btn btn-danger">创建收款账号</a>
          <?php }?>
          <a href="/finance/invoice/index" target="_blank" class="btn btn-warning">发票列表</a>
          <a href="/order/word/finance-excel?<?=http_build_query(\Yii::$app->request->queryParams)?>" target="_blank" class="btn btn-info down">下载财务单</a>
    	    <?= Html::a(Yii::t('app.finance', 'Create Finance'), ['create'], ['class' => 'btn btn-success']) ?>
    	</p>
	</h2>
	
    <?php echo $this->render('_search', ['model' => $searchModel]); ?>
    
		<?= GridView::widget([
	        'dataProvider' => $dataProvider,
	        //'filterModel' => $searchModel,
        	'layout'  =>  "{items}\n{pager}",
	        'columns' => [
	            //['class' => 'yii\grid\SerialColumn'],

            	[
            	    'attribute' => 'user_id',
            	    //'filter' =>  $user,
            	    'value' => function ($model) {
						return isset($model->user_id) ? $model->user->profile->name : '(未设置)' ;
            	    },
            	],
	            'code',
            	[
            	    'attribute' => 'price_type',
            	    //'filter' =>  Yii::$app->params['finance_price_type'],
            	    'value' => function ($model) {
            	        $type = Yii::$app->params['finance_price_type'];
            	        return $type[$model->price_type];
            	        },
            	    'format' => 'raw',
            	],
	            'company',
	            //'account',
	            'name',
            	[
            	    'attribute' => 'type',
            	    //'filter' =>  Yii::$app->params['finance_type'],
            	    'value' => function ($model) {
            	        	return  Yii::$app->params['finance_type'][$model->type];
            	    },
            	],
            	[
            	    'attribute' => 'status',
            	    //'filter' =>  Yii::$app->params['finance_status'],
            	    'value' => function ($model) {
            	        return Yii::$app->params['finance_status'][$model->status];
            	    },
            	    'format' => 'raw',
            	],
            	[
            	    'attribute' => '金额',
            	    'filter' =>  false,
            	    'value' => function ($model) {
            	        $price = Yii::$app->formatter->asCurrency($model->orderPrice);

            	        $price1 = Yii::$app->formatter->asCurrency($model->invoiceOrderPrice);
						return '<small> 金额:' . $price . '<br> 发票:' . $price1 . '</small>';
            	    },
            	    'format' => 'raw',
            	],
	            // 'created_at',
	            // 'updated_at',
                [
                    'label'=>'操 作',
                    'format'=>'raw',
                    'value' => function($model){
                        $url = sprintf("/finance/finance/view?id=%d", $model->id);
                        return Html::a('<i class="glyphicon glyphicon-eye-open"></i>',$url,['title' => '操作']);
                    }
                ],
            	[
            	    'label'=>'打印',
            	    'format'=>'raw',
            	    'value' => function($model){
						$url = sprintf("/finance/finance/print?id=%d", $model->id);
            			return Html::a('<i class="fa fa-print"></i>',$url,['title' => '打印', 'target'=>'_blank']);
            	    }
            	],
            	[
            	    'label'=>'审批',
            	    'format'=>'raw',
            	    'value' => function($model){
						if(\Yii::$app->user->can('is_finance') || \Yii::$app->user->can('is_admin')){
            			   $r =  urlencode(\yii::$app->request->getUrl());

            			   $url = sprintf("/finance/finance/finance-check?id=%d", $model->id);
            			   $str = Html::a('<i class="fa fa-check-square-o"></i>',$url,['title' => '审批', 'target'=>'_blank']);
            			   return $str;
            			}else{
							return " - ";
						}
            	    }
            	],

	        ],
	    ]); ?>
   
</div>
